package com.xxxy.hyc.xyxmp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xxxy.hyc.xyxmp.R;
import com.xxxy.hyc.xyxmp.bean.CartItem;

import java.util.List;

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.ViewHolder> {
    private Context context;
    private List<CartItem> cartItems;
    private OnCartItemChangeListener listener;

    public CartAdapter(Context context, List<CartItem> cartItems) {
        this.context = context;
        this.cartItems = cartItems;
    }

    public void setOnCartItemChangeListener(OnCartItemChangeListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_cart, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        CartItem item = cartItems.get(position);
        
        holder.tvName.setText(item.getGoods().getName());
        holder.tvPrice.setText("¥" + item.getGoods().getPrice());
        holder.tvQuantity.setText(String.valueOf(item.getQuantity()));
        
        // 设置增减按钮点击事件
        holder.btnDecrease.setOnClickListener(v -> {
            if (item.getQuantity() > 1) {
                item.setQuantity(item.getQuantity() - 1);
                holder.tvQuantity.setText(String.valueOf(item.getQuantity()));
                if (listener != null) {
                    listener.onQuantityChanged();
                }
            }
        });
        
        holder.btnIncrease.setOnClickListener(v -> {
            // 假设库存充足
            item.setQuantity(item.getQuantity() + 1);
            holder.tvQuantity.setText(String.valueOf(item.getQuantity()));
            if (listener != null) {
                listener.onQuantityChanged();
            }
        });
        
        holder.btnDelete.setOnClickListener(v -> {
            cartItems.remove(position);
            notifyDataSetChanged();
            if (listener != null) {
                listener.onItemDeleted();
            }
        });
    }

    @Override
    public int getItemCount() {
        return cartItems.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivGoods;
        TextView tvName, tvPrice, tvQuantity;
        Button btnDecrease, btnIncrease, btnDelete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivGoods = itemView.findViewById(R.id.iv_goods);
            tvName = itemView.findViewById(R.id.tv_name);
            tvPrice = itemView.findViewById(R.id.tv_price);
            tvQuantity = itemView.findViewById(R.id.tv_quantity);
            btnDecrease = itemView.findViewById(R.id.btn_decrease);
            btnIncrease = itemView.findViewById(R.id.btn_increase);
            btnDelete = itemView.findViewById(R.id.btn_delete);
        }
    }

    public interface OnCartItemChangeListener {
        void onQuantityChanged();
        void onItemDeleted();
    }
    
    // 计算总价
    public double getTotalPrice() {
        double total = 0;
        for (CartItem item : cartItems) {
            total += item.getSubtotal();
        }
        return total;
    }
}